package com.leantech.android.browser.ui.browser.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.viewbinding.ViewBinding
import coil.load
import com.leantech.android.browser.R
import com.leantech.android.browser.databinding.ItemBrowserTabFoldBinding
import com.leantech.android.browser.databinding.ItemBrowserTabGridBinding
import com.leantech.android.browser.databinding.ItemBrowserTabListBinding
import com.leantech.android.browser.skin.SkinManager
import com.leantech.android.browser.utils.onThrottleClick
import com.qmuiteam.qmui.util.QMUIResHelper

class TabPreviewAdapter(private val callback: PreviewTabItemCallback) :
    ListAdapter<TabPreviewItem, TabPreviewAdapter.BaseTabItemViewHolder>(
        diffCallback
    ) {

    companion object {
        const val ITEM_STYLE_CAROUSEL = 0
        const val ITEM_STYLE_GRID = 1
        const val ITEM_STYLE_LINEAR = 2

        private val diffCallback = object : DiffUtil.ItemCallback<TabPreviewItem>() {
            override fun areItemsTheSame(
                oldItem: TabPreviewItem, newItem: TabPreviewItem
            ): Boolean {
                return oldItem.tabIndex == newItem.tabIndex
            }

            override fun areContentsTheSame(
                oldItem: TabPreviewItem, newItem: TabPreviewItem
            ): Boolean {
                return oldItem == newItem
            }
        }
    }

    private var mCurItemStyle = ITEM_STYLE_GRID

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseTabItemViewHolder {
        return when (mCurItemStyle) {
            ITEM_STYLE_CAROUSEL -> CarouselTabItemViewHolder(
                ItemBrowserTabFoldBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )

            ITEM_STYLE_LINEAR -> LinearTabItemViewHolder(
                ItemBrowserTabListBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )

            else -> GridTabItemViewHolder(
                ItemBrowserTabGridBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: BaseTabItemViewHolder, position: Int) {
        holder.bind(getItem(position), callback)
    }

    abstract class BaseTabItemViewHolder(
        binding: ViewBinding
    ) : ViewHolder(binding.root) {
        abstract fun bind(data: TabPreviewItem, callback: PreviewTabItemCallback)
    }

    class GridTabItemViewHolder(
        private val binding: ItemBrowserTabGridBinding
    ) : BaseTabItemViewHolder(binding) {

        override fun bind(data: TabPreviewItem, callback: PreviewTabItemCallback) {
            binding.root.isSelected = data.current
            if (data.favicon == null) {
                val defaultFavicon = QMUIResHelper.getAttrDrawable(itemView.context, SkinManager.currentTheme(itemView.context), R.attr.app_skin_icon_default_favicon)
                binding.iconFavicon.load(defaultFavicon)
            } else {
                binding.iconFavicon.load(data.favicon)
            }

            binding.imagePreview.setRadio(1, 1)
            if (data.preview != null) {
                binding.imagePreview.load(data.preview)
            }

            binding.title.text = data.title
            binding.root.onThrottleClick {
                callback.onTabItemClick(data)
            }

            binding.btnClose.onThrottleClick {
                callback.onTabItemClose(data, bindingAdapterPosition)
            }
        }
    }

    class CarouselTabItemViewHolder(
        private val binding: ItemBrowserTabFoldBinding
    ) : BaseTabItemViewHolder(binding) {

        override fun bind(data: TabPreviewItem, callback: PreviewTabItemCallback) {
            binding.root.isSelected = data.current
            if (data.favicon == null) {
                val defaultFavicon = QMUIResHelper.getAttrDrawable(itemView.context, SkinManager.currentTheme(itemView.context), R.attr.app_skin_icon_default_favicon)
                binding.iconFavicon.load(defaultFavicon)
            } else {
                binding.iconFavicon.load(data.favicon)
            }

            binding.imagePreview.setRadio(1, 1)
            if (data.preview != null) {
                binding.imagePreview.load(data.preview)
            }

            binding.title.text = data.title
            binding.root.onThrottleClick {
                callback.onTabItemClick(data)
            }

            binding.btnClose.onThrottleClick {
                callback.onTabItemClose(data, bindingAdapterPosition)
            }
        }
    }

    class LinearTabItemViewHolder(
        private val binding: ItemBrowserTabListBinding
    ) : BaseTabItemViewHolder(binding) {

        override fun bind(data: TabPreviewItem, callback: PreviewTabItemCallback) {
            binding.root.isSelected = data.current
            if (data.favicon == null) {
                val defaultFavicon = QMUIResHelper.getAttrDrawable(itemView.context, SkinManager.currentTheme(itemView.context), R.attr.app_skin_icon_default_favicon)
                binding.iconFavicon.load(defaultFavicon)
            } else {
                binding.iconFavicon.load(data.favicon)
            }

            binding.imagePreview.setRadio(1, 1)
            if (data.preview != null) {
                binding.imagePreview.load(data.preview)
            }

            binding.title.text = data.title
            binding.root.onThrottleClick {
                callback.onTabItemClick(data)
            }

            binding.btnClose.onThrottleClick {
                callback.onTabItemClose(data, bindingAdapterPosition)
            }
        }
    }

    interface PreviewTabItemCallback {
        fun onTabItemClick(item: TabPreviewItem)

        fun onTabItemClose(item: TabPreviewItem, adapterPosition: Int)
    }

    fun setItemStyle(itemStyle: Int) {
        mCurItemStyle = itemStyle
        notifyDataSetChanged()
    }
}